<?php
/**
 * @brief attack_result protocol
 *
 * @param post uid User ID
 * @param post euid Enemy User ID
 * @return Result Battle, Exp, Coin, En
 * @author con100 <con100@playplus.net>
 * @since 12/04/2013
 * @copyright Copyright (c) 2013, PlayPlus
 **/

include 'dbconn.php';
include 'include.php';
include 'config.php';

$user_id = mysql_real_escape_string($_POST['uid']);
$enemy_id = mysql_real_escape_string($_POST['euid']);

$sql = "select no, level, sp_atk, sp_hit, sp_def, sp_spd, money, exp, max_exp, hp, max_hp, en, max_en from ebs_member where id='$user_id'";
$res = mysql_query($sql);
$data = mysql_fetch_assoc($res);

$sql = "select level, sp_atk, sp_hit, sp_def, sp_spd, hp from ebs_member where id='$enemy_id'";
$res = mysql_query($sql);
$enemy_data = mysql_fetch_assoc($res);
	
$user_stat = stat_conv($data);
$enemy_stat = stat_conv($enemy_data);

srand((double)microtime()*1000000);  

$my_hp = $data['hp'];
$enemy_hp = $enemy_data['hp'];
$battle_tern = $get_exp = $get_money = 0;
$my_battle_info = $enemy_battle_info = $battle_info = array();
$respawn_moon = _getMoonCount_($data);
//달빛의 축복 적용 여부
if($respawn_moon > 0){
	$ok_moon = 1;
}else{
	$ok_moon = 0;
}
//최초 각 유저 HP 정보 입력
$battle_info[] = array("my_hp"=>(int)$my_hp, "enemy_dmg"=>$enemy_damage,"enemy_hp"=>(int)$enemy_hp, "my_dmg"=>$damage);

while((($my_hp>0) AND ($enemy_hp>0))){
	$battle_tern++;
	//적 체력 감소
	$enemy_damage = 0;
	$r = min( max($user_stat['hit'] - $enemy_stat['spd'], -100), 100)/100*0.5+0.5;
	for($i=0;$i<10;$i++){
		if($r >= rand(0, 1000)/1000){
			$_enemy_damage = max($user_stat['atk'] - $enemy_stat['def']*0.8 + 3, 0) * 0.5;
			$_enemy_damage *= 0.66;//벨런스
			$enemy_damage += (int)$_enemy_damage;
		}
	}
	$enemy_hp -= $enemy_damage;
	if($enemy_hp <= 0){	
		$enemy_hp = 0;
	}
	//체력감소
	$damage = 0;
	$r = min( max($enemy_stat['hit'] - $user_stat['spd'], -100), 100)/100*0.5+0.5;
	for($i=0;$i<10;$i++){
		if($r >= rand(0, 1000)/1000){
			$_damage = max($enemy_stat['atk'] - $user_stat['spd']*0.8, 0) * 0.5;
			$_damage *= 0.66;//벨런스
			$damage += (int)$_damage;
		}
	}
	$my_hp -= $damage;
	if($my_hp <= 0){	
		$my_hp = 0;
	}
	$battle_info[] = array("my_hp"=>$my_hp, "enemy_dmg"=>$enemy_damage,"enemy_hp"=>$enemy_hp, "my_dmg"=>$damage);
//	$my_battle_info[] = array($my_hp, $enemy_damage);
//	$enemy_battle_info[] = array($enemy_hp, $damage);
	//돈 획득
	$battle_money = _getMoney_($data['level'], $enemy_data['level']);
	//경험치 획득
	$battle_exp = _getBattleExp_($my_hp, $enemy_hp, $data['level'], $enemy_data['level']);		
	//경험치 보정
	if($data['level'] > 10 && $enemy_damage == 0){
		$battle_exp = 0;
		$battle_money = 0;
	}
	if($ok_moon){
		$battle_money += $battle_money * 0.1;	
	}
	$get_exp += $battle_exp;
	$get_money += $battle_money;
}
//에너지 감소
if(!$ok_moon){
	$data['en'] -= $en_gamestart * $battle_tern;
}else{
	$data['en'] -= $en_gamestart_moon * $battle_tern;
}

$total_exp = $data['exp']+$get_exp;
$total_money = $data['money'] + $get_money;
$time_now = time();

if($total_exp >= $data['max_exp']){
	$data['level']++;
	if($data['level'] > 99){
		$data['level'] = 99;
		$total_exp = $data['max_exp'];
	}else{
		$levelup = 1;
		$total_exp -= $data['max_exp'];
		$max_exp = _getMaxExp_($data['level']);
		$total_exp = min($total_exp, $data['max_exp']-1);
	}
	$data['en'] = (int)$data['max_en'];
	$my_hp = (int)$data['max_hp'];
}

if($enemy_hp <= 0){	
	$killer_no  = $data['no'];
}
else{
	$killer_no  = null;
}
$result = array("Result"=>TRUE, "battle_info"=>$battle_info, "get_exp"=>$get_exp, "get_money"=>$get_money, "hp"=>$my_hp, "en"=>$data['en'], "levelup"=>$levelup, "level"=>$data['level'], "exp"=>$total_exp, "money"=>$total_money);
echo json_encode($result);
/*
$sql = "update ebs_member set exp='$total_exp', max_exp='$max_exp', money='$total_money', hp='$my_hp', en='$data[en]', level='$data[level]', timestemp_hp='$time_now', timestemp_active='$time_now', 
			timestemp_en='$time_now', killer_no=null where id='$user_id'";
$res = mysql_query($sql);

$sql = "update ebs_member set hp='$enemy_hp', timestemp_hp='$time_now', killer_no='$killer_no' where id='$enemy_id'";
$res = mysql_query($sql);
*/
/*
header('Content-type: text/xml; charset=UTF-8');

$writer = new XMLWriter();
$writer->openMemory();
$writer->setIndent(true);
$writer->setIndentString('  ');
$writer->startDocument('1.0', 'UTF-8');

$writer->startElement('attack_result');

	$writer->startElement('result');
	$writer->text($result);
	$writer->endElement();

	if($result == "TRUE"){
		$writer->startElement('tern_result');

		for($i=0;$i<$battle_tern;$i++){
			$writer->startElement('tern'.($i+1));				
				$writer->writeAttribute('my_dmg', $my_battle_info[$i][1]);
				$writer->writeAttribute('enemy_hp', $enemy_battle_info[$i][0]);
				$writer->writeAttribute('enemy_dmg', $enemy_battle_info[$i][1]);
				$writer->writeAttribute('my_hp', $my_battle_info[$i][0]);
			$writer->endElement();
		}
		$writer->endElement();

		$writer->startElement('get_exp');
		$writer->text($get_exp);
		$writer->endElement();

		$writer->startElement('get_money');
		$writer->text($get_money);
		$writer->endElement();

		$writer->startElement('hp');
		$writer->text($my_hp);
		$writer->endElement();

		$writer->startElement('en');
		$writer->text($en);
		$writer->endElement();

		$writer->startElement('levelup');
		if($levelup){
			$writer->text("TRUE");
		}else{
			$writer->text("FALSE");
		}
		$writer->endElement();
	}
$writer->endElement();

echo($writer->outputMemory());
/*
//hp 보정
$time_hp = (int)($data['hp']+(time()-$data['timestemp_hp']) * $hp_sec);
if($data['hp'] > 0){
	$data['hp'] = min($time_hp, $data['max_hp']);
}else if($time_hp > $data['max_hp']){
	$data['hp'] = $data['max_hp'];
}
$hp = $data['hp'];
$max_hp = $data['max_hp'];

//적 hp 보정
$time_hp = (int)($enemy_data['hp']+(time()-$enemy_data['timestemp_hp']) * $hp_sec);
if($enemy_data['hp'] > 0){
	$enemy_data['hp'] = min($time_hp, $enemy_data['max_hp']);
}else if($time_hp > $enemy_data['max_hp']){
	$enemy_data['hp'] = $enemy_data['max_hp'];
}
$enemy_hp = $enemy_data['hp'];
$enemy_max_hp = $enemy_data['max_hp'];

//EN 보정
if($data[timestemp_rest]){
	$rest_en = (int)($data[en]+(time()-$data[timestemp_rest]) * $en_rest_sec);
	$data[en] = min($rest_en, $data[max_en]);
	$sql = "update ebs_member set en='".$data[en]."', timestemp_rest=0, timestemp_en='".time()."' where id='$id'";
	$res = mysql_query($sql);
}else{
	$time_en = (int)($data[en]+(time()-$data[timestemp_en]) * $en_sec);
	$data[en] = min($time_en, $data[max_en]);	
}
$en = $data[en];
$max_en = $data[max_en];

//국가명
	$no_nation = $data['nation'];
	if($no_nation != 0){
		$sql = "select * from ebs_nation where no='$no_nation'";
		$res = mysql_query($sql);
		$nation = mysql_fetch_array($res);
	}else{
		$nation['name'] = "방랑자";
	}
	//적 국가명
	$enemy_no_nation = $enemy_data['nation'];
	if($enemy_no_nation != 0){
		$sql = "select * from ebs_nation where no='$enemy_no_nation'";
		$res = mysql_query($sql);
		$enemy_nation = mysql_fetch_array($res);
	}else{
		$enemy_nation['name'] = "방랑자";
	}	
	
	//장착 아이템
	$sql = "select type,value from ebs_inven where no='$data[no]' and equip=1";
	$res_inv = mysql_query($sql);
	while($row_inv = mysql_fetch_assoc($res_inv)){
		if(!$inven[$row_inv[type]]){
			$inven[$row_inv[type]] = $row_inv[value];
		}
	}	
	
	//적 장착 아이템
	$sql = "select type,value from ebs_inven where no='$enemy_data[no]' and equip=1";
	$res_inv = mysql_query($sql);
	while($row_inv = mysql_fetch_assoc($res_inv)){
		if(!$enemy_inven[$row_inv[type]]){
			$enemy_inven[$row_inv[type]] = $row_inv[value];
		}
	}
*/


?>